/** 
 * 项目名称:荣联生物云电商平台
 * 文件名:MyHttpClient.java 
 * 包名:com.ronglian.biocloud.commons
 * 日期:2016年5月9日 下午3:35:33 
 * Copyright (c) 2016, 北京荣之联科技股份有限公司 All Rights Reserved. 
 * 
 */
package com.ll.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 类名: MyHttpClient <br/>
 * 功能: http调用工具类<br/>
 * 日期: 2016年5月9日 下午3:35:33 <br/>
 * 
 * @author yubin
 *
 */
public class HttpClientUtils {
	
	private static final Logger LOG = LoggerFactory.getLogger(HttpClientUtils.class);
	
	/**
	 * 
	 * 功能: 发送GET请求<br/>
	 * 说明: <br/>
	 * 
	 * @author yubin
	 * @return String
	 * @throws IOException
	 * @throws ClientProtocolException
	 *
	 */
	public static String doGet(String uri) {
		CloseableHttpClient hc = HttpConnectionPool.getHttpClient();
		HttpGet request = new HttpGet(uri);
		CloseableHttpResponse response = null;
		String result = "";
		try {
			response = hc.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity httpEntity = response.getEntity();
				result = EntityUtils.toString(httpEntity);
				// 删除多余的字符,去掉返回结果中的"\r"字符，否则会在结果字符串后面显示一个小方格
				result.replaceAll("\r", "");
			} else {
				request.abort();
			}
		} catch (ClientProtocolException e) {
			LOG.error("HttpClientUtils.doGet,客户端协议异常",e);
			result = e.getMessage().toString();
		} catch (IOException e) {
			LOG.error("HttpClientUtils.doGet,IO异常",e);
			result = e.getMessage().toString();
		} catch (Exception e) {
			LOG.error("HttpClientUtils.doGet",e);
			result = e.getMessage().toString();
		}finally {
			try {
				request.releaseConnection();
				response.close();
			} catch (IOException e) {
				LOG.error("HttpClientUtils.doGet",e);
				result = e.getMessage().toString();
			}
		}
		return result;
	}

	/**
	 * 
	 * 功能: 发送POST请求<br/>
	 * 说明: <br/>
	 * 
	 * @author yubin
	 * @param uri
	 * @param params
	 * @return
	 * @return String
	 *
	 */
	public static String doPost(String uri, Map<String, String> params) {
		CloseableHttpClient hc = HttpConnectionPool.getHttpClient();
		HttpPost request = new HttpPost(uri);
		CloseableHttpResponse response = null;
		String result = "";
		try {
			List<NameValuePair> paramsList = new ArrayList<NameValuePair>();

			Set<String> keys = params.keySet();
			if (!keys.isEmpty()) {
				for (String key : keys) {
					paramsList.add(new BasicNameValuePair(key, params.get(key)));
				}
			}
			request.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));
			response = hc.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity httpEntity = response.getEntity();
				result = EntityUtils.toString(httpEntity);// 取出应答字符串
			}else {
				request.abort();
			}
		} catch (ClientProtocolException e) {
			LOG.error("HttpClientUtils.doPost,客户端协议异常",e);
			result = e.getMessage().toString();
		} catch (IOException e) {
			LOG.error("HttpClientUtils.doPost,IO异常",e);
			result = e.getMessage().toString();
		} catch (Exception e) {
			LOG.error("HttpClientUtils.doPost",e);
			result = e.getMessage().toString();
		} finally {
			try {
				request.releaseConnection();
				response.close();
			} catch (IOException e) {
				LOG.error("HttpClientUtils.doPost",e);
				result = e.getMessage().toString();
			}
		}
		return result;
	}
}
